package com.example.springboottest.example.xml.digester;

//import org.apache.commons.digester3.Digester;
//import org.apache.commons.digester3.annotations.DigesterRule;
import org.apache.tomcat.util.digester.Digester;
import org.springframework.util.ResourceUtils;

import java.io.File;

/**
 * digester采用sax事件解析的方式解析xml
 */
public class DigesterMain {
    public static void main(String[] args) {
        Digester digester = buildDigester();

        try {
            String path = ResourceUtils.getURL("src/main/java/com/example/springboottest/example/xml/digester/DigesterTest.xml").getPath();
            System.out.println(path);
            File file = new File(path);
            Department dept = (Department) digester.parse(file);
            System.out.println(dept.getName() + "\t" + dept.getCode());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void parse() {
        Digester digester = buildDigester();

        try {
            String path = ResourceUtils.getURL("src/main/java/com/example/springboottest/example/xml/digester/DigesterTest.xml").getPath();
            System.out.println(path);
            File file = new File(path);
            Department dept = (Department) digester.parse(file);
            System.out.println(dept.getName() + "\t" + dept.getCode());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Digester buildDigester() {
        Digester digester = new Digester();
        digester.setValidating(false);
//        digester.setr


        digester.addObjectCreate("department", Department.class.toString());
        digester.addSetProperties("department");
        return digester;
    }
}
